<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>项目管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 搜索筛选 -->
    <el-form :inline="true" class="user-search">
      <el-form-item label="搜索：">
      </el-form-item>
      <el-form-item label="" v-for="(filter, index) in searchParams.filters" :key="index">
        <el-input size="small" v-model="filter.fieldValue" v-bind:fieldName="filter.fieldName" v-if="filter.showAble"
                  v-bind:fieldValue="filter.fieldValue" v-bind:compare="filter.compare" :disabled="filter.readonly"
                  @keyup.enter.native="search"
                  :placeholder="filter.placeholder" :readonly="filter.readonly" clearable/>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleAdd()">添加</el-button>
      </el-form-item>
    </el-form>

    <!--列表-->
    <el-table size="small" :data="list" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中"
              style="width: 100%;" :header-row-style="{width: '100%', color: 'darkorange'}">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column prop="name" label="项目名称" width="300">
      </el-table-column>
      <el-table-column prop="code" label="项目编号" width="300">
      </el-table-column>
      <el-table-column prop="companyName" label="所属公司" width="300">
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteItem(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageParams" @callFather="callFather"/>


    <!-- 新增界面 -->
    <el-dialog :title="editTitle" :visible.sync="editFormVisible" :close-on-click-modal="false" width="30%" @click='closeDialog("edit")'>
      <el-form label-width="120px" :model="editForm" ref="editForm" :rules="rules">
        <el-form-item label="所属公司" prop="email">
          <el-input size="small" disabled v-model="editForm.companyName" placeholder="请输入所属公司"/>
        </el-form-item>
        <el-form-item label="项目名称" prop="remark">
          <el-input size="small" v-model="editForm.name" auto-complete="off" placeholder="请输入项目名称"/>
        </el-form-item>
        <el-form-item label="项目编号" prop="roleName">
          <el-input size="small" v-model="editForm.code" auto-complete="off" placeholder="请输入项目编号"/>
        </el-form-item>
        <el-form-item label="备注" prop="roleName">
          <el-input size="small" v-model="editForm.remark" auto-complete="off" placeholder="请输入备注信息"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeDialog("edit")'>取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitAddForm('editForm')">保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {projectAdd, projectDelete, projectEdit, projectList} from "../../../api/baseMG";
  import Pagination from '../../../components/Pagination'
  import {getStore} from "../../../utils/util";

  export default {
    data() {
      return {
        list: [],
        editFormVisible: false,
        editTitle: "",
        loading: false,
        editForm: {},
        rules: {},
        // 分页参数
        pageParams: {
          currentPage: 1,
          pageSize: 10,
          total: 10
        },
        searchParams: {
          page: 1,
          perPage: 10,
          filters: [
            {
              showAble: false,
              readonly: true,
              placeholder: "默认",
              fieldName: "companyId",
              compare: "=",
              fieldValue: getStore("userInfo").companyId
            },
            {
              showAble: true,
              readonly: true,
              placeholder: "请输入公司名称",
              fieldName: "companyName",
              compare: "=",
              fieldValue: getStore("userInfo").company
            },
            {
              showAble: true,
              readonly: false,
              placeholder: "请输入项目名称",
              fieldName: "name",
              compare: "like",
              fieldValue: ""
            },
            {
              showAble: true,
              readonly: false,
              placeholder: "请输入项目编号",
              fieldName: "code",
              compare: "like",
              fieldValue: ""
            }
          ],
          isLock: ''
        }

      }
    },
    created() {
      this.search();

    },
    // 注册组件
    components: {
      Pagination
    },
    methods: {
      // 关闭编辑、增加弹出框
      closeDialog(dialog) {
        if (dialog === 'edit') {
          this.editFormVisible = false
        } else if (dialog === 'perm') {
          this.dataAccessshow = false
        } else if (dialog === 'unit') {
          this.unitAccessshow = false
        }
      },
      submitAddForm(params) {
        this.$refs[params].validate(valid => {
            if (valid) {
              if (!this.editForm.id) {
                projectAdd(this.editForm).then(resp => {
                  if (resp.success) {
                    this.$message({
                      type: 'success',
                      message: resp.msg
                    });
                    this.editFormVisible = false;
                    this.search();
                  }
                })
              } else {
                projectEdit(this.editForm).then(resp => {
                  if (resp.success) {
                    this.$message({
                      type: 'success',
                      message: resp.msg
                    });
                    this.editFormVisible = false;
                    this.search();
                  }
                })
              }
            }
          }
        );
      },
      handleAdd() {
        this.editTitle = "新增项目";
        this.editForm = {
          companyId: getStore("userInfo").companyId,
          companyName: getStore("userInfo").company,
          name: "",
          code: "",
          remark: ""
        };
        this.editFormVisible = true
      },
      // 分页插件事件
      callFather(param) {
        this.searchParams.page = param.currentPage;
        this.searchParams.perPage = param.pageSize;
        this.getdata(this.searchParams);
      },
      handleEdit(index, row) {
        this.editTitle = "修改";
        this.editForm = {
          id: row.id,
          companyName: row.companyName ? row.companyName : getStore("userInfo").company,
          companyId: row.companyId ? row.companyId : getStore("userInfo").companyId,
          name: row.name,
          code: row.code,
          remark: row.remark
        };
        this.editFormVisible = true
      },
      search() {
        this.getList(this.searchParams);
      },
      getList(params) {
        this.loading = true;
        projectList(params).then(resp => {
          if (resp.success) {
            this.loading = false;
            this.list = resp.data.list;
            this.pageParams.currentPage = resp.data.pageNum;
            this.pageParams.pageSize = resp.data.pageSize;
            this.pageParams.total = resp.data.total;
          }
        })
      },
      deleteItem(index, row) {
        this.$confirm('确定要删除吗?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          projectDelete(row.id).then(res => {
            if (res.success) {
              this.$message({
                type: 'success',
                message: res.msg
              });
              this.search(this.formInline)
            } else {
              this.$message({
                type: 'info',
                message: res.msg
              })
            }
          })
            .catch(err => {
              this.loading = false;
              this.$message.error('删除失败，请稍后再试！')
            })
        })
          .catch(err => {
            console.error(err);
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      }
    }
  }
</script>

<style scoped>
  .user-search {
    margin-top: 20px;
  }

  .userRole {
    width: 100%;
  }
</style>
